using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Custom;
using PlugNT.Cms.Model;
//5^1^a^s^p^x
namespace PlugNT.Cms.BLL
{
    public class Tables
    {

        private static readonly PlugNT.Cms.DAL.Tables dal = new PlugNT.Cms.DAL.Tables();

        private static Dictionary<string, string> tabDic = new Dictionary<string, string>();

        private void ReLoadDic()
        {
            tabDic.Clear();
            DataTable dt = dal.GetDicpathList("");
            foreach (DataRow dr in dt.Rows)
            {
                string dicpath = dr["dicpath"].ToString();
                string tmppath = (dicpath == "") ? "/" : "/" + dicpath + "/";
                tabDic.Add(dr["postfix"].ToString(), tmppath);
            }
        }

        public static string GetDicPath(string postfix)
        {
            string str = "";
            tabDic.TryGetValue(postfix, out str);
            return str;
        }






        public TablesInfo GetTablesInfo(int id)
        {
            return dal.GetTablesInfo(id);
        }







        public TablesInfo GetTablesInfoByPostfix(string postfix)
        {
            return dal.GetTablesInfoByPostfix(StringHelper.SqlFilter(postfix));

            
        }





        public TablesInfo GetAccessTablesInfo()
        {
            return dal.GetAccessTablesInfo();
        }






        public bool Add(TablesInfo model)
        {
            bool flag= dal.Add(model);
            if (flag)
            {
                string tmppath = (model.dicpath == "") ? "/" : "/" + model.dicpath + "/";
                tabDic.Add(model.postfix.Trim(), tmppath);
            }
            return flag;
        }






        public bool Update(TablesInfo model)
        {
            bool flag = dal.Update(model);
            if (flag)
            {
                if (tabDic.ContainsKey(model.postfix))
                {
                    string tmppath = (model.dicpath == "") ? "/" : "/" + model.dicpath + "/";
                    tabDic[model.postfix] = tmppath;
                }
            }
            return flag;
        }







        public bool Delete(int id)
        {
            bool flag = true;
            string postfix = dal.GetSingle("postfix", " and id=" + id.ToString()).ToString();
            if (postfix != "" && tabDic.ContainsKey(postfix))
            {
                flag=tabDic.Remove(postfix);
                if (flag)
                {
                    flag = dal.Delete(id);
                }
            }
            return flag;
        }






        public DataTable GetList( )
        {
            return dal.GetList("");
        }


        public int GetCount()
        {
            return dal.GetCount("");
        }

        public bool Exists(string  postfix)
        {
            return dal.GetCount(" and postfix='" + StringHelper.SqlFilter(postfix) +"'") > 0;
        }


        public string Help()
        {
            return @"mailto:354990393@qq.com";
        }


    }
}
